<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in employeeList" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="doSubmit">确定</el-button>
        <el-button size="small" @click="doCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployees } from '@/api/employees.js'
import { addDepartment, editDepartment, getDepartmentDetails } from '@/api/departments.js'

export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 部门编码校验规则
    const validCode = (rule, value, callback) => {
      console.log(value)

      let originCode = this.originList.map(item => item.code)
      console.log(originCode)

      // 编辑状态下需要排除自身部门编号
      originCode = this.originList.filter(item => item.id !== this.id).map(item => item.code)

      if (originCode.includes(value)) {
        callback(new Error(value + ' 已存在'))
      } else {
        callback()
      }
    }

    // 部门名校验
    const validName = (rule, value, callback) => {
      let originName = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      console.log(originName)

      // 编辑时不能与同级部门同名
      if (this.isEdit) {
        const el = this.originList.find(item => item.id === this.id)
        const brothersName = this.originList.filter(item => el.pid === item.pid && item.id !== this.id).map(item => item.name)

        originName = brothersName
        console.log('originName', originName)
      }

      if (originName.includes(value)) {
        callback(new Error(value + ' 已存在'))
      } else {
        callback()
      }
    }

    return {
      form: {
        name: '',
        code: '',
        introduce: '',
        manager: ''
      },
      employeeList: [], // 负责人列表
      // 表单校验规则
      rules: {
        name: [
          { required: true, message: '部门名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名为1-50个字符', trigger: ['blur', 'change'] },
          { validator: validName }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码为1-50个字符', trigger: ['blur', 'change'] },
          { validator: validCode }
        ],
        introduce: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍为1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  // watch: {
  //   id() {
  //     if (this.isEdit) {
  //       this.loadDepartmentDetails()
  //     }
  //   }
  // },
  async created() {
    // 加载负责人列表
    const res = await getEmployees()
    console.log(res)
    this.employeeList = res.data

    // if (this.isEdit) {
    //   this.loadDepartmentDetails()
    // }
  },
  methods: {
    // 获取部门详细信息
    async loadDepartmentDetails() {
      const data = await getDepartmentDetails(this.id)
      this.form = data.data
    },
    // 新增部门
    async doAdd() {
      addDepartment({ ...this.form, pid: this.id })
      this.$message.success('新增成功')
    },
    // 编辑部门
    async doEdit() {
      await editDepartment(this.form)
      this.$message.success('编辑成功')
    },
    // 点击提交按钮新增/编辑
    async doSubmit() {
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
          this.$emit('closeWindow')
        }
      })
    },
    doCancel() {
      this.$emit('closeWindow')
    },
    // 重置表单
    resetForm() {
      this.$refs.deptForm.resetFields()
    }
  }
}
</script>

<style>

</style>
